<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <child1></child1>
        <child2></child2>
    </div>
    <script src="../js/vue.js"></script>
    <script>
        const child1 = {
            data() {
                return {
                    msg:'这是要传递给组件2的值'
                }
            },
            template: `<div>
                <button @click="sendData">点击</button></div>`,

            methods: {
                sendData() {
                    this.$bus.$emit('getdata', this.msg)
                }
            }
        }

        const child2 = {
            template: `<div>这是子组件二</div>`,

            created() {
                this.$bus.$on('getdata', (params) => {
                    console.log(params);
                })
            },
        }
        // bus是一个具备事件传递能力的对象
        var bus = new Vue()
        Vue.prototype.$bus = bus
        new Vue({
            el: '#app',
            components: {
                child1,
                child2
            }
        })
    </script>
</body>
</html>